<?php
/**
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/11
 */

namespace app\merchant\model\system;

use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

/**
 * Class Express
 * @package app\admin\model\system
 */
class Express extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'express';

    use ModelTrait;

    public static function systemPage($params,$mer_id=0)
    {
        $model = new self;
        if ($params['keyword'] !== '') $model = $model->where('name|code', 'LIKE', "%$params[keyword]%");
        if($mer_id){
            self::firstCome($mer_id);
            $model = $model->where('mer_id',$mer_id);
        }
        $model = $model->order('sort DESC,id DESC');
        return self::page($model, $params);
    }

    /**
     * 第一次进来
     */
    public static function firstCome($mer_id){
        $is_have = self::be(['mer_id'=>$mer_id]);
        if(!$is_have){
            $all_data = self::where('mer_id',0)->select()->toArray();
            $insertAll = [];
            foreach ($all_data as $k=>$v){
                unset($all_data[$k]['id']);
                $all_data[$k]['mer_id'] = $mer_id;
                $insertAll[] = $all_data[$k];
            }
            $system_user_level_model = new self();
            $system_user_level_model->saveAll($insertAll);
        }
    }
}